package qeb.handheldmedical.download;

import qeb.handheldmedical.R;
import qeb.handheldmedical.exam.LineExam;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author 作者(dugaoyi) E-mail:dugaoyi@163.com
 * @version 创建时间：2016年9月2日 上午9:45:30
 * @declaration 下载
 */
public class DownloadActivity extends Activity {

	private TextView text;
	private ProgressBar load;
	/** 文件名 */
	private String fileName, titleName;
	private Button btn_know;
	private static DownloadActivity mdownActivity;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.download);
		WindowManager m = getWindowManager();
		Display d = m.getDefaultDisplay(); // 为获取屏幕宽、高
		LayoutParams p = getWindow().getAttributes(); // 获取对话框当前的参数值
		// p.height = (int) (d.getHeight() * 0.3); // 高度设置为屏幕的1.0
		p.width = (int) (d.getWidth() * 0.8); // 宽度设置为屏幕的0.8
		p.alpha = 1.0f; // 设置本身透明度
		p.dimAmount = 0.0f; // 设置黑暗度
		getWindow().setAttributes(p);
		// 设置为true点击区域外消失
		setFinishOnTouchOutside(false);

		titleName = getIntent().getExtras().getString("titleName");
		this.setTitle("【" + titleName + "】正在下载…………");

		text = (TextView) findViewById(R.id.tvloadName);

		load = (ProgressBar) findViewById(R.id.load);

		btn_know = (Button) findViewById(R.id.btn_know);
		btn_know.setOnClickListener(btnKnow_onClickListener);
		btn_know.setVisibility(View.GONE);

	}

	private OnClickListener btnKnow_onClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub Activity.RESULT_OK,
			DownloadActivity.this.setResult(Activity.RESULT_OK, getIntent());
			DownloadActivity.this.finish();
		}
	};

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onStart()
	 */
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub

		// 获取传递的Intent的Bundle的url键值
		final String url = getIntent().getExtras().getString("url");
		fileName = getIntent().getExtras().getString("fileName");
		final Handler handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				// 这里就一条消息
				int pro = load.getProgress() + msg.arg1;
				load.setProgress(pro);
				text.setText(Integer.toString(pro));
				if (pro >= load.getMax()) {
					DownloadActivity.this.setResult(Activity.RESULT_OK,
							getIntent());
					DownloadActivity.this.finish();
				}
			}
		};

		new Thread(new Runnable() {
			@Override
			public void run() {
				// 另起线程执行下载，安卓最新sdk规范，网络操作不能再主线程。
				try {
					Download l = new Download(url);

					load.setMax(l.getLength());

					/**
					 * 下载文件到sd卡，虚拟设备必须要开始设置sd卡容量
					 * downhandler是Download的内部类，作为回调接口实时显示下载数据
					 */
					int status = l.down2sd("qebdown/", fileName,
							l.new downhandler() {
								@Override
								public void setSize(int size) {
									Message msg = handler.obtainMessage();
									msg.arg1 = size;
									msg.sendToTarget();
								}
							});

				} catch (Exception ex) {
					DownloadActivity.this.setResult(100, getIntent());
					DownloadActivity.this.finish();
				}

			}
		}).start();

		super.onStart();
	}

}
